package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.bean.QueryBean;
import com.chhuang.ewcrm.entity.EOther;
import com.chhuang.ewcrm.service.EOtherService;
import com.chhuang.ewcrm.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (EOther)表控制层
 *
 * @author chhuang
 * @since 2020-11-16 10:31:51
 */
@RestController
@RequestMapping("/service/eOther")
public class EOtherController {
    Logger logger = LoggerFactory.getLogger(EOtherController.class);
    /**
     * 服务对象
     */
    @Resource
    private EOtherService eOtherService;


    /**
     * 确认记录完成
     * @param otherWeighId
     * @param remark2
     * @return
     */
    @PostMapping("/confirmCompletion")
    public ResultBean<?> confirmCompletion(String otherWeighId, String remark2){
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);

        if(StringUtils.isNotEmpty(otherWeighId)) {
            EOther eOther = this.eOtherService.queryById(otherWeighId);
            if (eOther != null) {
                eOther.setRemark2(remark2);
                eOther.setStatus(2);
                this.eOtherService.update(eOther);
                resultBean.setSuccess(true);
            }else {
                resultBean.setMessage("称重记录不存在");
            }
        }else {
            resultBean.setMessage("参数不为空");
        }

        return resultBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param otherWeighId 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    public ResultBean<EOther> getOne(String otherWeighId) {
        ResultBean<EOther> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);
        if(StringUtils.isNotEmpty(otherWeighId)){
            resultBean.setSuccess(true);
            resultBean.setData(this.eOtherService.queryById(otherWeighId));
        }else{
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 表格查询
     * @param queryBean
     * @return
     */
    @PostMapping("/queryPage")
    public ResultBean<PageInfo<EOther>> query(@RequestBody QueryBean<EOther> queryBean){
        logger.debug(queryBean.toString());
        ResultBean<PageInfo<EOther>> resultBean = new ResultBean<>();

        PageInfo<EOther> pageInfo = null;
        List<EOther> others = null;
        if(queryBean!=null) {
            if (StringUtils.isNotEmpty(queryBean.getOrderBy()))
                pageInfo = eOtherService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize(), queryBean.getOrderBy());
            else
                pageInfo = eOtherService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize());
            resultBean.setData(pageInfo);
            resultBean.setSuccess(true);
            resultBean.setCode(queryBean.getDraw());
        }else {
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }

        logger.debug(resultBean.toString());
        return resultBean;
    }

}